package cn._2dland.util;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * HMAC-SHA1签名算法
 * 
 * @author deadblue
 */
public class HMAC_SHA1 extends SignatureAlgorithm {

	/** 算法内部名 */
	private static final String ALGORITHM_NAME = "HmacSHA1";

	@Override
	public String signature(byte[] key, byte[] content) {
		String sign = null;
		try {
			// 初始化算法
			SecretKey sKey = new SecretKeySpec(key, ALGORITHM_NAME);
			Mac mac = Mac.getInstance(ALGORITHM_NAME);
			mac.init(sKey);

			// 执行签名
			byte[] buf = mac.doFinal(content);
			// 将结果用Base64编码
			sign = Base64.encode(buf);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sign;
	}

}
